import { quitIfWebGPUNotAvailable } from ".";

// Extend the Navigator interface to include the 'gpu' property
export async function initWebGpu(el: HTMLCanvasElement) {
  // implementation goes here
  console.log("GPU Tools initialized");

  const adapter = await navigator.gpu?.requestAdapter({
    featureLevel: "compatibility",
  });
  const device = await adapter?.requestDevice();
  if (!device) {
    console.error("WebGPU not supported");
    return;
  }
  quitIfWebGPUNotAvailable(adapter, device);
  const context = el.getContext("webgpu") as GPUCanvasContext | null;

  if (!context) {
    console.error("WebGPU context not supported");
    return;
  }
  const devicePixelRatio = window.devicePixelRatio;
  el.width = el.clientWidth * devicePixelRatio;
  el.height = el.clientHeight * devicePixelRatio;
  const presentationFormat = navigator.gpu.getPreferredCanvasFormat();

  context.configure({
    device,
    format: presentationFormat,
  });
  return { context, device, adapter, presentationFormat };
}
